package com.magesti.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.magesti.DAO.DaoFactory;
import com.magesti.DAOinterface.HistoriqueInterfaceDAO;
import com.magesti.DAOinterface.InterfaceGestionMail;
import com.magesti.DAOinterface.OperationInterfaceDAO;
import com.magesti.beans.Operation;
import com.magesti.exception.DAOException;
import com.magesti.form.ModifieOperationForm;
import com.magesti.gestionnaire.GestionRole;
import com.magesti.ressource.Generated;
import com.magesti.ressource.Global;
import com.magesti.ressource.ServletGlobal;

@SuppressWarnings("serial")
public class ModifieOperation extends HttpServlet {

	private OperationInterfaceDAO operationDAO;
	private HistoriqueInterfaceDAO historiqueDAO;
	private InterfaceGestionMail gestionnaireMail;
	private GestionRole gestionRole;

	//private String PARAM_AFFICHE_OPERATION = "?"+Global.ATT_REQUEST_ID+"=";

	public void init() throws ServletException {
		DaoFactory f = ((DaoFactory) getServletContext().getAttribute(
				Global.ATT_CONTEXT_DAO_FACTORY));
		operationDAO = f.getOperationDAO();
		historiqueDAO = f.getHistoriqueDAO();
		gestionnaireMail = f.getGestionnaireMail();
		gestionRole = new GestionRole();
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
		 request.setAttribute(Global.ATT_REQUEST_LISTE_DGA_DEMANDEUSE,Generated.VALEURS_DGA_DEMANDEUSE);	
			
		int id_op = ServletGlobal.checkIdOperation(request);
		List<String> listEtats = Generated.VALEURS_ETAT_OPERATION;
		Operation op = null;
		String erreur = "";
		
		try {
			op = operationDAO.createOperation(id_op);
		} catch (DAOException e) {
			erreur = "Trace de l'erreur:" + e.getMessage();
		} catch (Exception e){
			erreur = "Erreur de l'identifiant. " + e.getMessage();
		}
		
		ServletGlobal.setRights(op, request, gestionRole);
		
		request.setAttribute(Global.ATT_REQUEST_OPERATION,op);
		request.setAttribute(Global.ATT_REQUEST_ERREUR,erreur);
		request.setAttribute(Global.ATT_REQUEST_ETATS_OPERATION,listEtats);
		ServletGlobal.setRightsUser(request);
		
		this.getServletContext().getRequestDispatcher(Global.VUE_JSP_MODIFIE_OPERATION).forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		 request.setAttribute(Global.ATT_REQUEST_LISTE_DGA_DEMANDEUSE,Generated.VALEURS_DGA_DEMANDEUSE);	
			
		List<String> listEtats = Generated.VALEURS_ETAT_OPERATION;
		ModifieOperationForm form = new ModifieOperationForm(operationDAO,historiqueDAO,gestionnaireMail,gestionRole);
		Operation op = form.modifieOperation(request);
		
		ServletGlobal.setRights(op, request, gestionRole);
		
		request.setAttribute(Global.ATT_REQUEST_ETATS_OPERATION,listEtats);
		request.setAttribute(Global.ATT_REQUEST_OPERATION,op);
		request.setAttribute(Global.ATT_REQUEST_FORM,form);
		ServletGlobal.setRightsUser(request);
		
		this.getServletContext().getRequestDispatcher(Global.VUE_JSP_MODIFIE_OPERATION).forward(request,response);
		//response.sendRedirect(request.getContextPath() + Global.VUE_JSP_MODIFIE_OPERATION + PARAM_AFFICHE_OPERATION);
	}
	
}
